package com.dllo.lanou.cookapp.utils;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;

import com.dllo.lanou.cookapp.BaseApplication;
import com.dllo.lanou.cookapp.entity.User;

import java.io.File;
import java.util.Calendar;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UpdateListener;
import cn.bmob.v3.listener.UploadFileListener;

/**
 * Created by xijinhua on 2016/3/9.
 */
public class CutPictureUtil {
    private static  CutPictureUtil util;
   private static Context context;
    private User newuser,userinfo;
    public CutPictureUtil(){

    }
    public static CutPictureUtil getInstance(Context context)  {
        if(util == null){
            synchronized ((CutPictureUtil.class)){
                if (util == null){
                    util = new CutPictureUtil();
                    context = context;

                   userInfo = BmobUser.getCurrentUser(context, User.class);
                    Log.i("测试3",userInfo+"");
                }
            }
        }
        return util;
    }
    private File tempFile;

    public Intent getImageView(Uri uri) {
       Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");//查看类型 详细的类型在 com.google.android.mms.ContentType
        intent.putExtra("crop", true);
        ;// 才能出剪辑的小方框，不然没有剪辑功能，只能选取图片
        intent.putExtra("aspectX", 1);// 出现放大和缩小
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 300);
        intent.putExtra("outputY", 300);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        tempFile = new File("/sdcard/ll1x/" + Calendar.getInstance().getTimeInMillis() + ".jpg"); // 以时间秒为文件名
        File temp = new File("/sdcard/ll1x/");//自已项目 文件夹
        if (!temp.exists()) {
            temp.mkdir();
        }
        intent.putExtra("output", Uri.fromFile(tempFile));  // 专入目标文件
        intent.putExtra("outputFormat", "JPEG"); //输入文件格式
       //

        return  intent;
    }
    public  String setPicToView(Intent picdata,ImageView imageView) {
        Bundle extras = picdata.getExtras();
        if (extras != null) {
            Bitmap photo = extras.getParcelable("data");
            imageView.setImageBitmap(photo);

            //像云端上传头像/
        }
        String urlpath = tempFile.getAbsolutePath();


        return  urlpath;
    }
public static User userInfo;
    //修改BmobFile文件
    public void updateBmobFile(User u,User user2){
        //userInfo = BaseApplication.instance.getUser();
        Log.i("测试4",userinfo+"::::");
        Log.i("测试5",user2+"");
        u.update(context, user2.getObjectId(), new UpdateListener() {
            @Override
            public void onSuccess() {
                Log.i("更新用户图片", "更新用户成功");

            }

            @Override
            public void onFailure(int i, String s) {

            }
        });

    }
}
